package com.zuofei.xmpp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zuofei.xmpp.R;
import com.zuofei.xmpp.service.IMService;
import com.zuofei.xmpp.utils.ThreadUtils;
import com.zuofei.xmpp.utils.ToastUtils;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class LoginActivity extends AppCompatActivity {

    public static final String SERVICE_NAME = "itheima.com";
    private static String IP = "192.168.0.109";
    private static int PORT = 5222;
    private Button loginBtn;
    private EditText username;
    private EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifyUser();
            }
        });
    }

    /**
     * 验证用户
     */
    private void verifyUser() {
        final String username = this.username.getText().toString();
        final String password = this.password.getText().toString();
        if(TextUtils.isEmpty(username)){
            this.username.setError("用户名不能为空");
            return;
        }
        if(TextUtils.isEmpty(password)){
            this.password.setError("密码不能为空");
            return;
        }
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                connectServer(username,password);
            }
        });

    }

    /**
     * 连接openfire服务端
     * @param username
     * @param password
     */
    private void connectServer(String username, String password) {
        ConnectionConfiguration config = new ConnectionConfiguration(IP, PORT);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        config.setDebuggerEnabled(true);
        XMPPConnection conn = new XMPPConnection(config);
        try {
            conn.connect();
            conn.login(username,password);
            showMsgPanel();
            IMService.connection = conn;
            //保存当前登陆的账户
            username = username+"@"+LoginActivity.SERVICE_NAME;
            IMService.mCurAccount = username;
            //启动service
            Intent intent = new Intent(LoginActivity.this,IMService.class);
            startService(intent);
            ToastUtils.showToastSafe(LoginActivity.this,"登陆成功");

        } catch (XMPPException e) {
            e.printStackTrace();
            ToastUtils.showToastSafe(LoginActivity.this,"登陆失败");
        }
    }

    /**
     * 进入消息面板界面
     */
    private void showMsgPanel() {
        finish();
        Intent intent = new Intent(LoginActivity.this,MsgPanelActivity.class);
        startActivity(intent);
    }

    /**
     * 初始化视图
     */
    private void initView() {
        loginBtn = findViewById(R.id.loginBtn);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
    }
}
